package com.azunitech.apm.bundles.events;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

import com.azunitech.apm.common.api.IEventProxyService;
import com.azunitech.apm.common.api.Report;
/*
 * This implements the API from the 
 */
public class BundleEventProxyServiceImpl implements IEventProxyService {
	private BundleContext m_BundleContext;
	
	public BundleEventProxyServiceImpl(BundleContext context){
		this.m_BundleContext = context;
	}

	public void dispatch(Report report) {
		ServiceReference<?> ref = m_BundleContext.getServiceReference(EventAdmin.class.getName());
		if (ref != null)
		{
			EventAdmin eventAdminService = (EventAdmin) m_BundleContext.getService(ref);
			Dictionary<String, Object> properties = new Hashtable<String, Object>();
			properties.put("title", report.getTitle());
			properties.put("path" , report.getAbsolutePath());
			properties.put("time", System.currentTimeMillis());
		
			Event reportGeneratedEvent = new Event("com/acme/reportgenerator/GENERATED", properties);
			//eventAdminService.sendEvent(reportGeneratedEvent);
			eventAdminService.postEvent(reportGeneratedEvent);
		}
	}

	public Dictionary<String, Object> encount() {
		return null;
	}
}
